﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using TimKen.DataAccess;
using TimKen.DataEntity;
using TimKen.BussLogic;

namespace TimKen.Web.Admin.Authority
{
    public partial class Authority_Edit : TimKen.App.AdminPage
    {
        public override string ActionCode
        {
            get
            {
                return "8";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindResources();
                if (!string.IsNullOrEmpty(Request.QueryString["UserName"]))
                {
                    UserName = Request.QueryString["UserName"];
                    UserAccountManager manager = new UserAccountManager();
                    DataEntity.UserAccount  user =   manager.GetModel(UserName);
                    ActionCodeStr = user.ActionList;
                }
                else
                    Response.Redirect("DataAdmin_List.aspx");

                BindData();
            }
        }
        
        public void BindResources()
        {
            btnUpdate.Text = GetLangString("Update");
        }

        public string ActionCodeStr = "";

        public string UserName 
        {
            get
            {
                if (ViewState["UserName"] != null)
                    return ViewState["UserName"].ToString();
                return null;
            }
            set
            {
                ViewState["UserName"] = value;
            }
        }

        /// <summary>
        /// 获取所有的Action.存储在缓存里面
        /// </summary>
        /// <returns></returns>
        private DataSet GetAllAction()
        {
            DataSet ds = new DataSet();
            if (Cache.Get("ActionList") == null)
            {
                ds = DataAccess.DbHelperSQL.Query("SELECT  * FROM AdminAction");
                Cache.Insert("ActionList", ds);
            }
            else
                ds = Cache.Get("ActionList") as DataSet;

            return ds; 
        }



        public  DataTable GetChildAction (int ParentID)
        {
              DataSet  ds = GetAllAction();
             return   Convert(ds.Tables[0].Select(" ParentID="+ParentID));//父DataList绑定源
        }



        public DataTable Convert(DataRow[] drs) 
        {
            DataSet ds = GetAllAction();

            DataTable dt = ds.Tables[0].Copy();
            dt.Rows.Clear();

            foreach (DataRow dr in drs)
            {
                dt.ImportRow(dr);
            }

            return dt;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
             string  actionCode =   Request["actionCode[]"];

             ActionCodeStr = actionCode;
             UserAccountManager manager = new UserAccountManager();
             manager.UpdateDataSystemActionList(UserName, actionCode);
             Response.Redirect(Request.RawUrl);
        }

       private void BindData()
       {
           DataSet ds = GetAllAction();
           DataTable dt = ds.Tables[0].Copy();
           DataRow[] dr = ds.Tables[0].Select(" ParentID=0");//父DataList绑定源
           grdActionList.DataSource = Convert(dr);
           grdActionList.DataBind();//绑定
       }
     
    }
}
